define(function(require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('../../su/xsySuInfo/widget');

    require('core/widgets/itemsCRU/item');

    $.widget('rk.xsyCustomizeInfo', $.rk.xsySuInfo, {
        _params: {},
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            me._beIniting();
            me._loadData({}, function(result) {
                // result.uuid //控件的uuid
                // result.common //页面公共的数据
                // result.self//控件自己的数据
                // console.log('me._loadData finished', result);

                // result.self.entityData.id = void(0);
                elem.itemsinplace({
                    // elem.itemsform({
                    entities: result.self.entityData,
                    items: result.self.itemList,
                    savesuccess: function(evt, arg) {
                        // arg.element: 触发编辑的DOM元素
                        // arg.item: 编辑的字段
                        // arg.newValue: 保存成功的值
                        // arg.entity: 组件当前的全部数据
                        // arg.relation: 如果当前item是关联字段，值为，该关联字段的引用字段

                        if (10 === arg.item.searchType) {
                            var merge = (function(items) {
                                return function(entity) {
                                    var key, rslt = {};
                                    items.forEach(function(item) {
                                        key = item.entryPropertyNameOnly;
                                        rslt[key] = entity.hasOwnProperty(key) ? entity[key] : null;
                                    });
                                    return rslt;
                                }
                            })(arg.relation);
                            if (arg.newValue) {
                                $.getJSON('/json/crm_customize/joinLink-detail.action', {
                                    referEntityInstId: arg.newValue.id, //是关联实体的ID
                                    referCustomizeId: arg.item.id //是关联实体的itemID
                                }).success(function(json) {
                                    json.status || elem.itemsinplace('update', $.extend(true, {
                                        id: arg.entity.id
                                    }, merge(json.data)));
                                }).fail(function() {

                                });
                            } else {
                                elem.itemsinplace('update', $.extend(true, {
                                    id: arg.entity.id
                                }, merge({})));
                            }
                        }

                        var response = arg.response;
                        var relateComputeItems = response.data.relateComputeItems;
                        /*var relateComputeItems = [{
                            item : {
                                entryPropertyNameOnly : 'ownerId'
                            },
                            newValue : '123'
                        },{
                            item : {
                                entryPropertyNameOnly : 'customItem5'
                            },
                            newValue : '123'
                        }]*/
                        var items = {}
                        items.id = arg.entity.id;
                        if(relateComputeItems && relateComputeItems.length && relateComputeItems.length >= 1){
                            relateComputeItems.forEach(function(item){
                                items[item.item.entryPropertyNameOnly] = item.newValue
                            })  
                            elem.itemsinplace('update', items);
                            arg.relateComputeItems = items;
                        }
                        

                        try {
                            //广播一个【外部】事件，控件以外会监听到
                            me._triggerOuterEvent('detailChanged', arg);
                        } catch (ex) {
                            console.log('trigger detailChanged error: ', ex);
                        }
                    },
                    savefail: function(evt, arg) {
                        if (arg.response.status == '300003') {
                            $.msg(arg.item.label + '重复');
                        }
                    }
                });


                // 收听【外部】控件广播而来的事件
                me._listenOuterEvent('afterPopupEdit', function(arg) {
                    me._loadData({}, function(result) {
                        //entityData中没有值的item补null
                        var itemList = result.self.itemList;
                        var entityData = result.self.entityData;
                        itemList.forEach(function(item){
                            entityData[item.entryPropertyNameOnly] != 0 &&  (!entityData[item.entryPropertyNameOnly]) && (entityData[item.entryPropertyNameOnly] = null);
                        })
                        elem.itemsinplace('update', result.self.entityData);
                    });
                });
            }, function() {
                console.log('loading data error');
            });
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        }
    });
});
